 /***UserData By GYLite 2022-12-26
 * 用户信息类，通常提供token、userId、服务器目录等固定的用户信息
*/
class UserData {
	private static _instance:UserData;	
	public static getInstance():UserData
	{
		if(UserData._instance == null)
			UserData._instance = new UserData;
		return UserData._instance;
	}

	public token:string="";
	public whiteUserIndex:number=-1;
    public userId:string="100000";	
	/**逻辑服务器根目录 可通过挂全局对象localHref:{root:地址}对象指定默认root*/
	public root:string;
	/**资源服务器根目录 可通过挂全局对象localHref:{ossRoot:地址}对象指定默认ossRoot*/
	public ossRoot:string;		
	public isRelease:number=0;//1 生产 0 开发 2 测试 3 灰度 4压测	参考ProjType常量
	public constructor() {

		let s= this;			
		let debug:boolean;
		let globalObj:any;
		globalObj = window;
		debug = GameManager.debug;
		s.root = debug?"":((globalObj.localHref && globalObj.localHref.root)?globalObj.localHref.root:URLConf.DEFAULT_ROOT);
		s.ossRoot = debug?"":((globalObj.localHref && globalObj.localHref.ossRoot)?globalObj.localHref.ossRoot:URLConf.DEFAULT_ROOT);
	}
	/**设置用户信息
	 * @param d {key:value}
	*/
	public setUserData(d:any):void
	{let s = this;
		for(let key in d)
		{
			if(d[key]!=null && key in s)
			{
				s[key] = d[key];
			}
		}
		if(d.frameRate)
		{
			GYLite.GYSprite.stage.frameRate = d.frameRate;
		}		
		s.whiteUserIndex = s.getWhiteUserIndex();
		Log.writeLog(MultiLang.str69 + s.isRelease + "-" + MultiLang.str70 + s.whiteUserIndex,Log.WARN);
		URLConf.resetRoot();
		HTTPConf.resetRoot();
		Log.tokenLog(s.token);
	}		
	private _webData:any = window["gy_webData"]?window["gy_webData"]:{};
	private _tempWebData:any = {};	
	private _originData:any = {};//初始url参数
	public getHostData(url:string):void{
		let arr:Array<string> = url.split("?");
		let i:number,len:number;
		let arr2:Array<string>;
		let s = this;
		if(arr.length > 1)
		{
			arr = arr[1].split("&");
			len = arr.length;
			for(i=0;i<len;++i)
			{
				arr2 = arr[i].split("=");
                if(arr2[0] == "isRelease" && s._webData.isRelease != null)continue;				
				s._originData[arr2[0]] = s._webData[arr2[0]] = arr2[1];
			}
		}		
        if(s._webData.isRelease != null)
			s.isRelease = s._webData.isRelease;
		if(s._webData.userId != null)
            s.userId = s._webData.userId;
		URLConf.resetRoot();
		HTTPConf.resetRoot();	
	}
	/**清理缓存的url参数*/
	public clearTempQuery():void
	{
		let key:string;
		let s = this;
		for(key in s._tempWebData)
		{			
			delete s._webData[key];
		}
		s._tempWebData = {};
		s.resetOriginData();
		URLConf.resetRoot();
	}
	/**重置初始url参数*/
	public resetOriginData():void
	{
		let key:string;
		let s = this;
		for(key in s._originData)
		{			
			s._webData[key] = s._originData[key];
		}
	}
	/**设置url参数，通过json键值
	 * @param data json键值
	 * @param temporary 是否临时(销毁模块后将被清理)
	*/
	public setQueryByJson(data:any, temporary:boolean):void
	{
		let key:string;
		let s = this;
		for(key in data)
		{
			if(temporary)
				s._tempWebData[key] = data[key];
			s._webData[key] = data[key];
		}
		URLConf.resetRoot();
	}
	/**获取原始url参数*/
	public getOriQueryVariable(key:string):any
	{
 	 	return this._originData[key];
 	}
	/**设置url参数*/
	public setQueryVariable(key:string,data:any):void
	{
		this._webData[key] = data;
	}
	/**获取url参数*/
	public getQueryVariable(key:string):any
	{
 	 	return this._webData[key];
 	}
	/**获取url参数 module名*/
	public getQueryModuleName():string
	{
		return this._webData["module"]?this._webData["module"].split("#")[0]:(<any>window).gy_module;
	}
	/**获取url参数 chartIndex环节索引*/
	public getQueryChartIndex():string
	{
		return this._webData["chartIndex"];
	}
	
	public getPlatform():string
	{		
		return this._webData["platform"]?this._webData["platform"]:"default";
	}
	
	/**获取url参数 appVersion app传过来的app版本号*/
	public getQueryAppVersion():string
	{
		return this._webData["appVersion"];
	}
	
	/**是否url参数debug*/
	public getUrlDebug():boolean
	{let s =this;
		return s._webData["debug"] == 1;
	}
	/**所属的白名单用户组索引，不是白名单返回-1*/
	public getWhiteUserIndex():number
	{
		let arr:string[][];
		let i:number,j:number,len:number,len2:number;
		let userId:string;
		let s = this;
		userId = s.userId;
		if(UserData.getInstance().isRelease == ProjType.DEV)
			arr = [];
		else
			arr = [];
		//[[],[],[跳过按钮加快, debugData数据]]
		len = arr.length;
		for(i=0;i<len;++i)
		{
			len2 = arr[i].length;
			for(j=0;j<len2;++j)
			{
				if(arr[i][j] == userId)
				{					
					return i;
				}
			}	
		}
		return -1;
	}
}